package main

import (
	"os"
	"log"
	"io/ioutil"
	"flag"
	"regexp"
	"runtime"
	"sync"
	"strings"
)



var (
	keyword string
	reg *regexp.Regexp
	DIR_SEP string = "/"
	wg *sync.WaitGroup
	//ignoreDirs []string
	ignoreExts []string = make([]string,0)
	ignoreDirs []string = make([]string,0)

	isFile = flag.Bool("f",false,"is only search filename")
	//ignoreExt = flag.String("ig-exts",".png,.gif,.jpg","ignore extenstions")
	//ignoreDir = flag.String("ig-dirs",".git","ignore dirs")

	ignoreExt = flag.String("ig-exts",".png,.gif,.jpg,.gz,.tar,.exe,.xls,.xlsx","ignore extenstions")
	ignoreDir = flag.String("ig-dirs",".git,.idea","ignore dirs")
	max_read_file_size  = flag.Float64("max-read-file",2.0,"max read file search")
)



func main() {
	flag.Parse()
	wg = &sync.WaitGroup{}
	if(runtime.GOOS == "windows"){
		DIR_SEP = "\\"
	}


	//ignoreDirName := flag.String("ig-dir","","ignoreDirName")
	//ignoreDirs = strings.Split(*ignoreDirName , ",")
	ignoreExts = strings.Split(*ignoreExt , ",")
	ignoreDirs = strings.Split(*ignoreDir , ",")
	args := flag.Args()
	if len(args) < 1 {
		log.Fatal("缺少参数")
	}
	keyword = args[0]
	reg = regexp.MustCompile("(?i)"+keyword)
	//fmt.Println(isFile,keyword)
	//fmt.Println("os,exts,ext",runtime.GOOS,ignoreExts)
	dir , err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}

	fileinfos , err := ioutil.ReadDir(dir)
	if err != nil {
		log.Fatal(err)
	}
	if *isFile {
		searchFile(dir ,fileinfos , wg)
	}else{
		searchText(dir ,fileinfos , wg)
	}

	wg.Wait()

}
